package com.ruoyi.project.mq;

import com.google.gson.Gson;
import com.ruoyi.project.app.entity.SmsEntity;
import com.ruoyi.project.constant.MqConstants;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author hry
 * 向队列发送消息
 */
@Component
public class MqSender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public static MqSender mqSender;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void init() {
        mqSender = this;
        mqSender.rabbitTemplate = this.rabbitTemplate;
    }

    public void smsSend(SmsEntity smsEntity) {

        Gson gson = new Gson();
        String s = gson.toJson(smsEntity);

        /**
         * 参数一:交换机的名称  参数二:routing key,把消息交给符合指定routing key 的队列  参数三:消息
         */
        mqSender.rabbitTemplate.convertAndSend(MqConstants.TEST_EXCHANGE,
                MqConstants.TEST_KEY,s);
    }


}
